package com.lzl.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzl.common_utils.JwtUtils;
import com.lzl.common_utils.R;
import com.lzl.common_utils.ordervo.CourseWebVoOrder;
import com.lzl.eduservice.client.OrderClient;
import com.lzl.eduservice.entity.EduCourse;
import com.lzl.eduservice.entity.EduTeacher;
import com.lzl.eduservice.entity.chapter.ChapterVo;
import com.lzl.eduservice.entity.frontVo.CourseFrontVo;
import com.lzl.eduservice.entity.frontVo.CourseWebVo;
import com.lzl.eduservice.service.EduChapterService;
import com.lzl.eduservice.service.EduCourseService;
import com.lzl.eduservice.service.EduTeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {

    @Autowired
    private EduCourseService eduCourseService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private OrderClient orderClient;

    /**
     * 前台条件查询带分页查询课程
     * @param page
     * @param limit
     * @RequestBody(required = false)  表示courseFrontVo不一定需要有值，空值也能传
     * @param courseFrontVo
     * @return
     */
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page,
                                @PathVariable long limit,
                                @RequestBody(required = false) CourseFrontVo courseFrontVo){

        Page<EduCourse> pageCourse = new Page<>(page,limit);
        //返回map集合
        Map<String,Object> map = eduCourseService.getCourseFrontList(pageCourse,courseFrontVo);

        return R.ok().data("map",map);

    }

    //根据课程id查询前台课程详细信息
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId,
                                HttpServletRequest request){

        //根据课程id，编写sql语句查询课程详细信息
        CourseWebVo courseWebVo = eduCourseService.getBaseCourseInfo(courseId);

        //根据课程id 查询章节和小节----之前做过
        List<ChapterVo> chapterVideoList = eduChapterService.getChapterVideoByCourseId(courseId);

        //根据课程id和用户id 查询 当前课程是否已经支付
        boolean buyCourse = orderClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));


        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList).data("isBuyCourse",buyCourse);
    }


    //被远程调用---根据课程id查询课程信息
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){

        CourseWebVo courseInfo = eduCourseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,courseWebVoOrder);

        return courseWebVoOrder;

    }





}
